package com.hxzy.servlet.servletContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author nick
 */
@WebServlet("/context")
public class MyServletContext extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取servlet上下文环境,对应的是jsp内置对象application
        ServletContext application = req.getServletContext();
        System.out.println("servlet上下问内存地址：" + application);
        resp.sendRedirect("index.jsp");

        //得到发布到tomcat的项目名称
        String contextPath = application.getContextPath();
        System.out.println("contextPath:\t" + contextPath);
        String projectName = application.getInitParameter("projectName");
        System.out.println("获取application初始化参数：" + projectName);

    }
}
